package com.dingreading.cloud.web.service.impl;

import com.dingreading.cloud.web.dto.ReadstarStudentDto;
import com.dingreading.cloud.web.entity.ReadstarStudent;
import com.dingreading.cloud.web.entity.table.ReadstarStudentTableDef;
import com.dingreading.cloud.web.mapper.ReadstarStudentMapper;
import com.dingreading.cloud.web.service.ReadstarStudentService;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.dingreading.cloud.web.entity.table.ReadstarPayOrderTableDef.readstarPayOrder;
import static com.dingreading.cloud.web.entity.table.StoresTableDef.stores;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2025-03-24
 */
@Service
public class ReadstarStudentServiceImpl extends BaseServiceImpl<ReadstarStudentMapper, ReadstarStudent, ReadstarStudentTableDef> implements ReadstarStudentService {

    @Override
    protected ReadstarStudentTableDef getTable() {
        return ReadstarStudentTableDef.readstarStudent;
    }

    @Override
    public ReadstarStudent getByUid(String uid) {
        return getMapper().selectOneByCondition(table.uid.eq(uid));
    }

    @Override
    public boolean exitPhoneAndUserName(String uid, String phone, String userName) {
        QueryCondition condition = table.phone.eq(phone)
                .and(table.userName.eq(userName));
        if (StringUtils.isNotBlank(uid))
            condition.and(table.uid.ne(uid));
        return exists(condition);
    }

    @Override
    public long getCount(String openid) {
        return getMapper().selectCountByCondition(table.openid.eq(openid));
    }

    @Override
    public List<ReadstarStudentDto> listByOpenid(String openid) {
        QueryWrapper wrapper = QueryWrapper.create()
                .select(table.allColumns, stores.address, readstarPayOrder.payStatus)
                .from(table)
                .leftJoin(stores).on(table.storeUid.eq(stores.uid))
                .leftJoin(readstarPayOrder).on(table.orderUid.eq(readstarPayOrder.orderUid))
                .where(table.openid.eq(openid));
        return getMapper().selectListByQueryAs(wrapper, ReadstarStudentDto.class);
    }

    @Override
    public ReadstarStudentDto getByOpenidAndStudentUid(String openid, String studentUid) {
        QueryWrapper wrapper = QueryWrapper.create()
                .select(table.allColumns, stores.address, readstarPayOrder.payStatus)
                .from(table)
                .leftJoin(stores).on(table.storeUid.eq(stores.uid))
                .leftJoin(readstarPayOrder).on(table.orderUid.eq(readstarPayOrder.orderUid))
                .where(table.openid.eq(openid).and(table.uid.eq(studentUid)));
        return getMapper().selectOneByQueryAs(wrapper, ReadstarStudentDto.class);
    }

    @Override
    public void updateOrderUid(String studentUid, String orderUid) {
        UpdateChain.of(ReadstarStudent.class)
                .set(table.orderUid, orderUid)
                .where(table.uid.eq(studentUid))
                .update();
    }
}
